package com.xjrsoft.module.language.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.result.Response;
import com.xjrsoft.core.tool.utils.BeanUtil;
import com.xjrsoft.core.tool.utils.StringPool;
import com.xjrsoft.module.base.entity.XjrBaseModule;
import com.xjrsoft.module.base.entity.XjrBaseModuleColumn;
import com.xjrsoft.module.base.entity.XjrBaseModuleForm;
import com.xjrsoft.module.base.service.IXjrBaseModuleColumnService;
import com.xjrsoft.module.base.service.IXjrBaseModuleFormService;
import com.xjrsoft.module.base.service.IXjrBaseModuleService;
import com.xjrsoft.module.language.dto.LgTypeListDto;
import com.xjrsoft.module.language.dto.LgTypeDto;
import com.xjrsoft.module.language.entity.XjrLgType;
import com.xjrsoft.module.language.service.IXjrLgTypeService;
import com.xjrsoft.module.language.vo.LgTypeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 多语言语言类型表 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2020-11-11
 */
@RestController
@AllArgsConstructor
@RequestMapping("/lg-type")
@Api(value = "/lg-type", tags = "语言类型模块")
public class XjrLgTypeController {

    private final IXjrLgTypeService lgTypeService;

    private final IXjrBaseModuleColumnService columnService;

    private final IXjrBaseModuleFormService formService;

    private final IXjrBaseModuleService moduleService;

    /**
    * @Author:湘北智造-框架开发组
    * @Date:2020/11/12
    * @Param:[id]
    * @return:com.xjrsoft.common.result.Response
    * @Description:获取详情
    */
    @GetMapping("/{id}")
    @ApiOperation(value = "获取详情")
    public Response getLgTypeById(@PathVariable String id) {
        return Response.ok(lgTypeService.getById(id));
    }

    /**
    * @Author:湘北智造-框架开发组
    * @Date:2020/11/12
    * @Param:[id, xjrLgType]
    * @return:com.xjrsoft.common.result.Response
    * @Description:修改
    */
    @PutMapping("/{id}")
    @ApiOperation(value = "修改")
    public Response updateLgType(@PathVariable String id, @RequestBody LgTypeDto lgTypeDto) {
        XjrLgType lgType = BeanUtil.copy(lgTypeDto, XjrLgType.class);
        lgType.setId(id);
        XjrLgType oldType = lgTypeService.getById(id);
        XjrBaseModule module = moduleService.getOne(Wrappers.<XjrBaseModule>query().lambda()
                .eq(XjrBaseModule::getEnCode, "LgMapManager")
                .eq(XjrBaseModule::getEnabledMark, 1));
        columnService.update(Wrappers.<XjrBaseModuleColumn>update().lambda()
                .set(XjrBaseModuleColumn::getEnCode, lgTypeDto.getCode())
                .set(XjrBaseModuleColumn::getFullName, lgTypeDto.getName())
                .eq(XjrBaseModuleColumn::getModuleId, module.getModuleId())
                .eq(XjrBaseModuleColumn::getEnCode, oldType.getCode()));
        formService.update(Wrappers.<XjrBaseModuleForm>update().lambda()
                .set(XjrBaseModuleForm::getEnCode, lgTypeDto.getCode())
                .set(XjrBaseModuleForm::getFullName, lgTypeDto.getName())
                .eq(XjrBaseModuleForm::getModuleId, module.getModuleId())
                .eq(XjrBaseModuleForm::getEnCode, oldType.getCode()));
        return Response.status(lgTypeService.updateById(lgType));
    }

    /**
    * @Author:湘北智造-框架开发组
    * @Date:2020/11/12
    * @Param:[id, xjrLgType]
    * @return:com.xjrsoft.common.result.Response
    * @Description:设置为主语言
    */
    @PutMapping("/{id}/main")
    @ApiOperation(value = "设置为主语言")
    public Response setMainLanguage(@PathVariable String id) {
        return Response.status(lgTypeService.setMainlanguage(id));
    }

    /**
    * @Author:湘北智造-框架开发组
    * @Date:2020/11/12
    * @Param:[id]
    * @return:com.xjrsoft.common.result.Response
    * @Description:获取列表数据
    */
    @GetMapping("/page")
    @ApiOperation(value = "获取列表数据")
    public Response<PageOutput<LgTypeVo>> getPageData(LgTypeListDto dto) {
        return Response.ok(lgTypeService.getPageData(dto));
    }

    @GetMapping
    @ApiOperation(value = "获取列表数据")
    public Response<List<LgTypeVo>> getDataList(@RequestParam(required = false) String keyword) {
        List<XjrLgType> dataList = lgTypeService.getDataList(keyword);
        return Response.ok(BeanUtil.copyList(dataList, LgTypeVo.class));
    }

    /**
    * @Author:湘北智造-框架开发组
    * @Date:2020/11/12
    * @Param:[id, lgTypeDto]
    * @return:com.xjrsoft.common.result.Response
    * @Description:新增
    */
    @PostMapping
    @ApiOperation(value = "新增")
    public Response saveLgType(@RequestBody LgTypeDto lgTypeDto) {
        XjrLgType lgType = BeanUtil.copy(lgTypeDto, XjrLgType.class);
        XjrBaseModule module = moduleService.getOne(Wrappers.<XjrBaseModule>query().lambda()
                .eq(XjrBaseModule::getEnCode, "LgMapManager")
                .eq(XjrBaseModule::getEnabledMark, 1));
        if (module != null) {
            // 列表字段
            XjrBaseModuleColumn column = new XjrBaseModuleColumn();
            column.setEnCode(lgTypeDto.getCode());
            column.setModuleId(module.getModuleId());
            column.setFullName(lgTypeDto.getName());
            column.setParentId(StringPool.ZERO);
            columnService.save(column);
            // 表单字段
            XjrBaseModuleForm form = new XjrBaseModuleForm();
            form.setEnCode(lgTypeDto.getCode());
            form.setModuleId(module.getModuleId());
            form.setFullName(lgTypeDto.getName());
            form.setParentId(StringPool.ZERO);
            formService.save(form);
        }
        return Response.status(lgTypeService.save(lgType));
    }

    /**
    * @Author:湘北智造-框架开发组
    * @Date:2020/11/12
    * @Param:[xjrLgType]
    * @return:com.xjrsoft.common.result.Response
    * @Description:删除
    */
    @DeleteMapping("/{ids}")
    @ApiOperation(value = "删除")
    public Response deleteType(@PathVariable String ids) {
        XjrBaseModule module = moduleService.getOne(Wrappers.<XjrBaseModule>query().lambda()
                .eq(XjrBaseModule::getEnCode, "LgMapManager")
                .eq(XjrBaseModule::getEnabledMark, 1));
        List<String> idList = Arrays.asList(ids.split(","));
        List<XjrLgType> lgTypeList = lgTypeService.list(Wrappers.<XjrLgType>query().lambda().in(XjrLgType::getId, idList));
        List<String> codeList = lgTypeList.stream().map(lgType -> lgType.getCode()).collect(Collectors.toList());
        columnService.remove(Wrappers.<XjrBaseModuleColumn>query().lambda().eq(XjrBaseModuleColumn::getModuleId, module.getModuleId()).in(XjrBaseModuleColumn::getEnCode, codeList));
        formService.remove(Wrappers.<XjrBaseModuleForm>query().lambda().eq(XjrBaseModuleForm::getModuleId, module.getModuleId()).in(XjrBaseModuleForm::getEnCode, codeList));
        return Response.status(lgTypeService.removeByIds(idList));
    }

    @GetMapping("/main")
    @ApiOperation(value = "获取默认主语言")
    public Response<LgTypeVo> getMainLgType() {
        XjrLgType lgType = lgTypeService.getOne(Wrappers.<XjrLgType>query().lambda().eq(XjrLgType::getIsMain, 1), false);
        if (lgType == null) {
            return Response.notOk("当前系统没有设置主语言！");
        }
        return Response.ok(BeanUtil.copy(lgType, LgTypeVo.class));
    }

}
